package com.example.hrmsaas.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hrmsaas.system.entity.Loginfo;
import com.example.hrmsaas.system.entity.User;
import com.example.hrmsaas.system.mapper.LoginfoMapper;
import com.example.hrmsaas.system.mapper.UserMapper;
import com.example.hrmsaas.system.service.ILoginfoService;
import com.example.hrmsaas.system.utils.DataGridView;
import com.example.hrmsaas.system.utils.vo.LoginfoVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 雷哥
 * @since 2021-07-16
 */
@Service
public class LoginfoServiceImpl extends ServiceImpl<LoginfoMapper, Loginfo> implements ILoginfoService {

    private Log log = (Log) LogFactory.getLog(LoginfoServiceImpl.class);
    @Autowired
    LoginfoMapper loginfoMapper;

    @Autowired
    UserMapper  userMapper;

    @Transactional(readOnly = true)
    @Override
    public DataGridView getLogInfoList(LoginfoVo loginfoVo) {
        QueryWrapper<Loginfo> queryWrapper = new QueryWrapper<>();
        if(null != queryWrapper){
            queryWrapper.like(StringUtils.isNotBlank(loginfoVo.getLoginname()), "loginname",loginfoVo.getLoginname());
            queryWrapper.ge(loginfoVo.getStartDate() != null, "logintime", loginfoVo.getStartDate());
            queryWrapper.le(loginfoVo.getEndDate() != null, "logintime", loginfoVo.getEndDate());
        }else{
            log.error("logInfoVo为空");
        }
        queryWrapper.orderByDesc("logintime");
        Page<Loginfo> page = new Page<>(loginfoVo.getPage(), loginfoVo.getLimit());
        this.loginfoMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public User getUserByMobile(String mobile) {
        QueryWrapper<User> models = new QueryWrapper<User>();
        models.eq(StringUtils.isNotBlank(mobile),"mobile",mobile);
        return userMapper.selectOne(models);
    }


}
